//
//  AuctionSoreShopVC.m
//  MyNewProject
//
//  Created by 王春辉 on 2020/1/4.
//  Copyright © 2020年 sun. All rights reserved.
//

#import "AuctionSoreShopVC.h"
#import "AuctionSoreShopCollectionCell.h"
#import "AuctionSoreDetailVC.h"
#import "ProductDetailVC.h"
#import "AuctionScoreConverListVC.h"

#pragma mark -- mark 数据相关
#import "AuctionScoreShopModel.h"

#define MARGIN 15.0
@interface AuctionSoreShopVC ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> {
    UILabel *_soreLabel;
    UILabel *_moneyLabel;
    
    int _page;
}

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UICollectionView *collectionView;


@property (nonatomic, strong) NSArray <AuctionScoreShopDataModel *>*dataArray;
@end
static NSString *const identifier = @"cell_identifier";
@implementation AuctionSoreShopVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_backStyle = GKNavigationBarBackStyleWhite;
    self.gk_navBackgroundColor = SKClearColor;
    self.view.backgroundColor = SKColorWithHex(0xF1F1F1);
    [self initContentViews];
    [self requestShopJifenList];
}

- (void)initContentViews {
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(-SKStatusBar_Height);
        make.left.right.mas_offset(0);
        make.bottom.mas_offset(-SKBottomSafe_Height);
    }];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_offset(0);
        make.width.mas_equalTo(self.view);
    }];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestShopJifenList)];
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reuqestMoreShopJifenList)];
}

#pragma mark -- mark 懒加载
- (UIView *)headerView {
    if (!_headerView) {
        _headerView = [[UIView alloc] init];
        [self.collectionView addSubview:_headerView];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:SKIMAGE_NAMED(@"auction_sore_shop_header_img")];
        [_headerView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.mas_offset(0);
        }];
        
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = [UIColor whiteColor];
        view.layer.masksToBounds = true;
        view.layer.cornerRadius = 33.0;
        [_headerView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(15);
            make.right.mas_offset(-15);
            make.height.mas_equalTo(66);
            make.centerY.mas_equalTo(imageView.mas_bottom).mas_offset(0);
            make.bottom.mas_offset(0);
        }];
        
        UILabel *label1 = [[UILabel alloc] init];
        label1.text = @"我的积分";
        label1.textColor = [UIColor whiteColor];
        label1.font = SKFONT_Medium(15);
        [_headerView addSubview:label1];
        
        UILabel *coreLabel = [[UILabel alloc] init];
        coreLabel.textColor = [UIColor whiteColor];
        coreLabel.font = SKFONT_BOLD_SIZE(44);
//        coreLabel.text = @"1237";
        NSString *score = [NSString stringWithFormat:@"%d", [[UserInfoSingleton shareInstance].userInfo.jifen intValue]];
        coreLabel.text = score;
        [_headerView addSubview:coreLabel];
        
        UILabel *desLabel = [[UILabel alloc] init];
        desLabel.font = SKFONT_Medium(12);
        desLabel.textColor = SKColorWithHex(0xFEC6D0);
//        desLabel.text = @"积分等值金额≈123.7元";
        NSString *text = [NSString stringWithFormat:@"%.1f", [score floatValue] / 10];
        desLabel.text = [NSString stringWithFormat:@"积分等值金额≈%@元", text];
        [_headerView addSubview:desLabel];
        
        [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(coreLabel);
            make.bottom.mas_equalTo(coreLabel.mas_top).mas_offset(-4);
        }];
        [coreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(view);
            make.centerY.mas_equalTo(imageView);
            make.right.mas_lessThanOrEqualTo(imageView).mas_offset(0);
        }];
        [desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(coreLabel).mas_offset(0);
            make.top.mas_equalTo(coreLabel.mas_bottom).mas_offset(4);
        }];
        
        UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn1 setTitle:@" 积分详情" forState:UIControlStateNormal];
        btn1.titleLabel.font = SKFONT_BOLD_SIZE(17);
        [btn1 setTitleColor:SKColorWithHex(0x4C4C4C) forState:UIControlStateNormal];
        [btn1 setImage:SKIMAGE_NAMED(@"auction_sore_shop_detail_btn_img") forState:UIControlStateNormal];
        [btn1 addTarget:self action:@selector(soreDetailButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn1];
        
        UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn2 setTitle:@" 兑换记录" forState:UIControlStateNormal];
        btn2.titleLabel.font = SKFONT_BOLD_SIZE(17);
        [btn2 setTitleColor:SKColorWithHex(0x4C4C4C) forState:UIControlStateNormal];
        [btn2 setImage:SKIMAGE_NAMED(@"auction_sore_shop_exchange_log_btn_img") forState:UIControlStateNormal];
        [btn2 addTarget:self action:@selector(exchangeLogListButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn2];
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = self.view.backgroundColor;
        [view addSubview:line];
        [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(line.mas_left).mas_offset(-20);
            make.centerY.mas_equalTo(line);
        }];
        
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_offset(0);
            make.top.mas_offset(20);
            make.width.mas_equalTo(0.5);
        }];
        
        [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(line.mas_right).mas_offset(20);
            make.width.mas_equalTo(btn1);
            make.centerY.mas_equalTo(line);
        }];
        _soreLabel = coreLabel;
        _moneyLabel = desLabel;
    }
    return _headerView;
}
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumLineSpacing = 5;
        layout.minimumInteritemSpacing = 5;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        CGFloat w = (self.view.width - MARGIN * 2 - 5) * 0.5;
        CGFloat h = w / 170.0 * 228;
        layout.sectionInset = UIEdgeInsetsMake(MARGIN, MARGIN, MARGIN, MARGIN);
        layout.itemSize = CGSizeMake(w, h);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        [_collectionView registerClass:[AuctionSoreShopCollectionCell class] forCellWithReuseIdentifier:identifier];
        _collectionView.backgroundColor = self.view.backgroundColor;
        _collectionView.showsHorizontalScrollIndicator = false;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self.view addSubview:_collectionView];
    }
    return _collectionView;
}

#pragma mark -- mark UICollectionViewDelegate, UICollectionViewDataSource

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AuctionSoreShopCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    AuctionScoreShopDataModel *model = self.dataArray[indexPath.item];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:SKIMAGE_NAMED(@"cell_default_img")];
    cell.title = model.name;
    cell.price = model.jifen;
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    // 商品详情
    ProductDetailVC *vc = [[ProductDetailVC alloc] init];
    AuctionScoreShopDataModel *model = self.dataArray[indexPath.item];
    vc.ptId = model.ID;
    vc.type = 1;
    vc.showType = ProductDetailTypeDetail;
    [self.navigationController pushViewController:vc animated:true];
}

#pragma mark -- mark UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return self.headerView.size;
}

#pragma mark -- mark 获取数据

- (void)requestShopJifenList {
    _page = 1;
    SKWeakSelf;
    [self requestShopJifenListWith:_page Complete:^(NSArray<AuctionScoreShopDataModel *> *data) {
        weakSelf.dataArray = data;
        [weakSelf.collectionView reloadData];
    }];
}

- (void)reuqestMoreShopJifenList {
    if (_page == 0) {
        [self endRefreshing];
        return; }
    _page += 1;
    SKWeakSelf;
    [self requestShopJifenListWith:_page Complete:^(NSArray<AuctionScoreShopDataModel *> *data) {
        NSMutableArray <AuctionScoreShopDataModel *>*mArr = [NSMutableArray arrayWithArray:weakSelf.dataArray];
        [mArr addObjectsFromArray:data];
        weakSelf.dataArray = mArr;
        [weakSelf.collectionView reloadData];
    }];
}

- (void)requestShopJifenListWith:(int)page Complete:(void(^)(NSArray <AuctionScoreShopDataModel *>*))complete {
    NSString *score = [NSString stringWithFormat:@"%d", [[UserInfoSingleton shareInstance].userInfo.jifen intValue]];
    _soreLabel.text = score;
    NSString *text = [NSString stringWithFormat:@"%.1f", [score floatValue] / 10];
    _moneyLabel.text = [NSString stringWithFormat:@"积分等值金额≈%@元", text];
    SKWeakSelf;
    [YQNetworking getWithUrl:@"Shop/getJifenList" params:@{@"page":@(_page)} reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        AuctionScoreShopModel *model = [AuctionScoreShopModel modelWithDictionary:response];
        if ([model isSuccess]) {
            if (complete) {
                complete(model.data); }
        }else {
            [weakSelf showMessage:model.msg];
        }
        [weakSelf endRefreshing];
    } failBlock:^(NSError *error) {
        [weakSelf debugShowMessage:error.localizedDescription];
        [weakSelf endRefreshing];
    }];
}

- (void)endRefreshing {
    if ([self.collectionView.mj_header isRefreshing]) {
        [self.collectionView.mj_header endRefreshing];
    }
    if ([self.collectionView.mj_footer isRefreshing]) {
        [self.collectionView.mj_footer endRefreshing];
    }
}

#pragma mark -- mark EVENT

/**
 积分详情 点击事件

 @param sender sender
 */
- (void)soreDetailButtonClick:(UIButton *)sender {
    AuctionSoreDetailVC *vc = [[AuctionSoreDetailVC alloc] init];
    [self.navigationController pushViewController:vc animated:true];
}

/**
 兑换记录 点击事件

 @param sender sender
 */
- (void)exchangeLogListButtonClick:(UIButton *)sender {
    AuctionScoreConverListVC *vc = [[AuctionScoreConverListVC alloc] init];
    [self.navigationController pushViewController:vc animated:true];
}
#pragma mark -- mark 业务逻辑



@end
